// loneNozzleCommand.cpp: implementation of the CloneNozzleCommand class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "loneNozzleCommand.h"
#include "NozzleSelector.h"
#include "Interact.h"
#include "EquipSelector.h"
#include "NozzleGeometry.h"
#include "UEEntityOperation.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CloneNozzleCommand::CloneNozzleCommand()
{

}

CloneNozzleCommand::~CloneNozzleCommand()
{

}

BOOL CloneNozzleCommand::Execute()
{
	Nozzle* pNozzle = NULL;
	NozzleSelector select;
	if(select.Select())
	{
		pNozzle = select.GetSelected();
	}
	else 
	{
		return FALSE;
	}
	if ( pNozzle == NULL)
	{
		return FALSE;
	}
	EquipSelector eselect;
	Equip* pEquip = NULL;
	BOOL bResult = eselect.Select();
	if( !bResult )
	{
		return FALSE;
	}

	pEquip = eselect.GetSelected();
	if (pEquip == NULL)
	{
		return FALSE;
	}

	StdString strPrompt;
	strPrompt.LoadString(COMMAND_CREATENOZZLE_FIXPOS);

	AcGePoint3d ptPos;
	if (!CInteract::GetPoint(strPrompt,ptPos))
	{
		return FALSE;
	}

	this->m_pNozzle = (Nozzle*)(pNozzle->Clone());
	AcGePoint3d ptStart = this->m_pNozzle->GetFixPos();
	this->m_pNozzle->SetFixPos(ptPos);
	AcGeVector3d vMove = ptPos - ptStart;
	AcGeMatrix3d xform;
	xform.setToTranslation(vMove);
	NozzleGeometry* pG = (NozzleGeometry*)(this->m_pNozzle->GetCurGeometry());

	AcDbEntity* pEntity = CUEEntityOperation
		::OpenEntityWrite(pG->GetHandle());

	pEntity->transformBy(xform);
	pEntity->close();

	UeObjectId Id(0);
	this->m_pNozzle->Id(Id);

	if( pEquip->GetNozzleManager().AddNozzle( m_pNozzle )
		!= UESoft::eManagerSuccess)
	{
		return FALSE;	
	}
	return TRUE;
}

